package com.example.demo.controller;

import org.springframework.core.io.Resource;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/contract")
public class virController {

    @RequestMapping("/total")
    public String total(Model model) throws IOException {
        String uriTemplate = "https://gitee.com/dgut-sai/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";
        URI uri = UriComponentsBuilder.fromUriString(uriTemplate).build(42);

        RequestEntity<Void> requestEntity = RequestEntity.get(uri)
                .headers(httpHeaders -> httpHeaders.add("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36"))
                .build();

        ResponseEntity<Resource> response = new RestTemplate().exchange(requestEntity, Resource.class);
        //String responseHeader = response.getHeaders().getFirst("MyResponseHeader");
        Resource body = response.getBody();
        int sum=0,sum1=0;
        List<Data> list = new ArrayList<Data>();
        Reader in = new InputStreamReader(body.getInputStream());
        Iterable<CSVRecord> records = CSVFormat.RFC4180.withFirstRecordAsHeader().parse(in);
        for (CSVRecord record : records) {
            String state = record.get("Province/State");
            String country = record.get("Country/Region");
            //String total = record.get(record.size()-1);
            String now=record.get(record.size()-1);
            String prev=record.get(record.size()-2);
            int p1=Integer.parseInt(now);
            int p2=Integer.parseInt(prev);
            //System.out.println(state);
            sum=sum+p1;
            sum1=sum1+(p1-p2);
            Data data=new Data(state,country,p1,p1-p2);
            list.add(data);
        }
        //System.out.println(list);
        model.addAttribute("country", "全球");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        model.addAttribute("time", df.format(new Date()));
        model.addAttribute("sum", sum);
        model.addAttribute("sum1", sum1);
        model.addAttribute("list", list);
        return "totalEarth";
    }

    @RequestMapping("/country")
    public String total(HttpServletRequest request, Model model) throws IOException {
        String queryCountry = request.getParameter("country");

        String uriTemplate = "https://gitee.com/dgut-sai/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";
        URI uri = UriComponentsBuilder.fromUriString(uriTemplate).build(42);

        RequestEntity<Void> requestEntity = RequestEntity.get(uri)
                .headers(httpHeaders -> httpHeaders.add("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36"))
                .build();

        ResponseEntity<Resource> response = new RestTemplate().exchange(requestEntity, Resource.class);
        //String responseHeader = response.getHeaders().getFirst("MyResponseHeader");
        Resource body = response.getBody();

        List<Data> list = new ArrayList<Data>();
        //assert body != null;
        int sum=0,sum1=0;
        Reader in = new InputStreamReader(body.getInputStream());
        Iterable<CSVRecord> records = CSVFormat.RFC4180.withFirstRecordAsHeader().parse(in);
        for (CSVRecord record : records) {
            String country = record.get("Country/Region");
            if(country.equals(queryCountry)) {
                String state = record.get("Province/State");
                String now = record.get(record.size()-1);
                String prev=record.get(record.size()-2);
                int p1=Integer.parseInt(now);
                int p2=Integer.parseInt(prev);
                sum=sum+p1;
                sum1=sum1+(p1-p2);

                Data data = new Data(state, country, p1, p1-p2);
                list.add(data);
            }
        }
        System.out.println(sum);
        System.out.println(sum1);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(df.format(new Date()));
        model.addAttribute("time", df.format(new Date()));
        model.addAttribute("country", queryCountry);
        model.addAttribute("sum", sum);
        model.addAttribute("sum1", sum1);
        model.addAttribute("list", list);
        return "totalEarth";
    }
}
